package com.gopay.scheduler.job;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.domain.cbrmb.customs.CbRmbCustomsPayment;
import com.gopay.common.domain.cbrmb.customs.CustomsRequest;
import com.gopay.common.domain.cbrmb.customs.CustomsResponse;
import com.gopay.remote.eis.customs.CustomsRemoteService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.cbrmb.CbRmbCustomsPaymentService;

/**
 * 跨境人民币海关报关Job
 * 
 * @CbRmbCustomsPaymentDeclareJob.java
 * @Description: 跨境人民币，海关报关系统，每3分钟扫描待报关数据，调用报关接口
 * @author lailingyu
 * @2017-02-21  上午9:42:20 www.gopay.com.cn Inc.All rights reserved.
 */
public class CbRmbCustomsPaymentDeclareJob extends GopayBaseJob {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "cbRmbCustomsPaymentService")
    private CbRmbCustomsPaymentService cbRmbCustomsPaymentService;

    @Resource(name="customsRemoteServiceClient")
    private CustomsRemoteService customsRemoteService;

    @Override
    protected void doJobInternal() {
        logger.info("=====跨境人民币海关报关定时任务开始=====");
        List<CbRmbCustomsPayment> undeclareList = cbRmbCustomsPaymentService.getUndeclaredOrder();
        if (undeclareList != null && undeclareList.size() > 0) {
            for (CbRmbCustomsPayment cbRmbCustomsPayment : undeclareList) {
                // 调用报关接口
                CustomsRequest request = new CustomsRequest();
                request.setSequence(System.currentTimeMillis());
                Map<String, Object> value = new HashMap<String, Object>();
                value.put("appType", cbRmbCustomsPayment.getAppType());
                value.put("orderId", cbRmbCustomsPayment.getSq());
                Map<String, Object> reqData = new HashMap<String, Object>();
                reqData.put("s", "1");
                reqData.put("t", "01");
                reqData.put("v", value);
                request.setReqData(reqData);
                CustomsResponse response = customsRemoteService.callCustoms(request);
                logger.info("=====报关流水号：" + cbRmbCustomsPayment.getSq()
                            + "，报关响应码：" + response.getRespCode() + "=====");
            }
        }

        logger.info("=====跨境人民币海关报关定时任务结束，共报关" + undeclareList.size() + "条数据=====");
    }
}
